﻿using System.Collections.Generic;
using System.Linq;
using ATTools.Models;
using Orchard.Data;
using Orchard.Environment.Extensions;

namespace ATTools.Services
{
    [OrchardFeature("ATTools.VoiceCommands")]
    public class VoiceCommandService : IVoiceCommandService
    {
        private readonly IRepository<VoiceCommandRecord> _voiceCommandRepository;

        public VoiceCommandService(IRepository<VoiceCommandRecord> voiceCommandRepository) 
        {
            _voiceCommandRepository = voiceCommandRepository;
        }

        #region IVoiceCommandService Members

        public IEnumerable<VoiceCommandRecord> GetVoiceCommands()
        {
            return _voiceCommandRepository.Table.ToList();
        }

        public VoiceCommandRecord CreateVoiceCommand(string description, CommandType commandType, string commandData)
        {
            VoiceCommandRecord voiceCommand = new VoiceCommandRecord { Description = description, CommandType = (byte)commandType, CommandData = commandData};
            _voiceCommandRepository.Create(voiceCommand);
            return voiceCommand;
        }

        public VoiceCommandRecord GetVoiceCommand(int voiceCommandId)
        {
            return _voiceCommandRepository.Get(x => x.Id == voiceCommandId);
        }

        public void DeleteVoiceCommand(int voiceCommandId)
        {
            VoiceCommandRecord voiceCommand = GetVoiceCommand(voiceCommandId);
            if (voiceCommand != null)
                _voiceCommandRepository.Delete(voiceCommand);
        }

        public void UpdateVoiceCommand(int voiceCommandId, string description, CommandType commandType, string commandData)
        {
            VoiceCommandRecord voiceCommand = GetVoiceCommand(voiceCommandId);
            if (voiceCommand != null)
            {
                voiceCommand.Description = description;
                voiceCommand.CommandType = (byte)commandType;
                voiceCommand.CommandData = commandData;
            }
        }

        #endregion
    }
}